﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;

namespace HospitalSystem
{
    public partial class register : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(@"Data Source=SJ-PC\SQLEXPRESS;Initial Catalog=db0015;Integrated Security=True");
        SqlConnection conn2 = new SqlConnection(@"Data Source=SJ-PC\SQLEXPRESS;Initial Catalog=db00172;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }

       // protected void Button1_Click(object sender, EventArgs e)
       // {
           

         //   string date2 = TextBox1.Text;
       //     DateTime convertdate = DateTime.Parse(date2);
           
//
         //   SqlCommand CmdSql = new SqlCommand("INSERT INTO [date] (date) VALUES (@name)", conn);
           // conn2.Open();
          //  CmdSql.Parameters.AddWithValue("@name", convertdate);


        //    CmdSql.ExecuteNonQuery();
          //  conn2.Close();



       //     MessageBox("ลงทะเบียนเรียบร้อยครับ");

       // }

      
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
         
            SqlCommand CmdSql22 = new SqlCommand("SELECT GEO_NAME,GEO_ID FROM [geography]", conn);


            conn.Open();
            SqlDataReader reader22 = CmdSql22.ExecuteReader();
            DataTable dt2 = new DataTable();
            DropDownList3.Items.Clear();
            ListItem newItem4 = new ListItem();
            newItem4.Text = "<เลือกภาค>";
            newItem4.Value = "0";
            DropDownList3.Items.Add(newItem4);

            while (reader22.Read())
            {
                newItem4 = new ListItem();
                newItem4.Text = reader22["GEO_NAME"].ToString();
                newItem4.Value = reader22["GEO_ID"].ToString();
                DropDownList3.Items.Add(newItem4);
            }
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            SqlCommand CmdSql44 = new SqlCommand("SELECT PROVINCE_ID,GEO_ID,PROVINCE_NAME FROM [province] WHERE GEO_ID=@GEO_ID", conn);

            CmdSql44.Parameters.AddWithValue("@GEO_ID", DropDownList3.SelectedValue);
            conn.Open();
            SqlDataReader reader44 = CmdSql44.ExecuteReader();
            DataTable dt4 = new DataTable();
            DropDownList4.Items.Clear();
            ListItem newItem44 = new ListItem();
            newItem44.Text = "<เลือกจังหวัด>";
            newItem44.Value = "0";
            DropDownList4.Items.Add(newItem44);

            while (reader44.Read())
            {
                newItem44 = new ListItem();
                newItem44.Text = reader44["PROVINCE_NAME"].ToString();
                newItem44.Value = reader44["PROVINCE_ID"].ToString();
                DropDownList4.Items.Add(newItem44);
            }
        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            SqlCommand CmdSql22 = new SqlCommand("SELECT AMPHUR_ID,AMPHUR_NAME FROM [amphur] WHERE PROVINCE_ID=@PROVINCE_ID", conn);

            CmdSql22.Parameters.AddWithValue("@PROVINCE_ID", DropDownList4.SelectedValue);
            conn.Open();
            SqlDataReader reader5 = CmdSql22.ExecuteReader();
            DataTable dt5 = new DataTable();
            DropDownList5.Items.Clear();
            ListItem newItem5 = new ListItem();
            newItem5.Text = "<เลือกอำเภอ>";
            newItem5.Value = "0";
            DropDownList5.Items.Add(newItem5);

            while (reader5.Read())
            {
                newItem5 = new ListItem();
                newItem5.Text = reader5["AMPHUR_NAME"].ToString();
                newItem5.Value = reader5["AMPHUR_ID"].ToString();
                DropDownList5.Items.Add(newItem5);
            }
        }

        protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            SqlCommand CmdSql22 = new SqlCommand("SELECT DISTRICT_CODE,DISTRICT_NAME FROM [district] WHERE AMPHUR_ID=@AMPHUR_ID", conn);

            CmdSql22.Parameters.AddWithValue("@AMPHUR_ID", DropDownList5.SelectedValue);
            conn.Open();
            SqlDataReader reader6 = CmdSql22.ExecuteReader();
            DataTable dt6 = new DataTable();
            DropDownList6.Items.Clear();
            ListItem newItem6 = new ListItem();
            newItem6.Text = "<เลือกอำตำบล>";
            newItem6.Value = "0";
            DropDownList6.Items.Add(newItem6);

            while (reader6.Read())
            {
                newItem6 = new ListItem();
                newItem6.Text = reader6["DISTRICT_NAME"].ToString();
                newItem6.Value = reader6["DISTRICT_CODE"].ToString();
                DropDownList6.Items.Add(newItem6);
            }
        }

        protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            SqlCommand CmdSql222 = new SqlCommand("SELECT zipcode FROM [zipcode] WHERE DISTRICT_CODE=@DISTRICT_CODE", conn);
            conn.Open();
            CmdSql222.Parameters.AddWithValue("@DISTRICT_CODE", DropDownList6.SelectedValue);
            SqlDataReader reader2 = CmdSql222.ExecuteReader();

            if (reader2.Read())
            {
                TextBox7.Text = reader2["zipcode"].ToString();

            }
            else
            {
                MessageBox("ไม่พบเลขบัตรครับ");
            }
            conn.Close();
            reader2.Close();





            SqlCommand CmdSql8 = new SqlCommand("SELECT GEO_NAME,GEO_ID FROM [geography]", conn);


            conn.Open();
            SqlDataReader reader8 = CmdSql8.ExecuteReader();
            DataTable dt2 = new DataTable();
            DropDownList8.Items.Clear();
            ListItem newItem8 = new ListItem();
            newItem8.Text = "<เลือกภาค>";
            newItem8.Value = "0";
            DropDownList8.Items.Add(newItem8);

            while (reader8.Read())
            {
                newItem8 = new ListItem();
                newItem8.Text = reader8["GEO_NAME"].ToString();
                newItem8.Value = reader8["GEO_ID"].ToString();
                DropDownList8.Items.Add(newItem8);
            }

            conn.Close();


        }

        protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            SqlCommand CmdSql9 = new SqlCommand("SELECT PROVINCE_ID,GEO_ID,PROVINCE_NAME FROM [province] WHERE GEO_ID=@GEO_ID", conn);

            CmdSql9.Parameters.AddWithValue("@GEO_ID", DropDownList8.SelectedValue);
            conn.Open();
            SqlDataReader reader9 = CmdSql9.ExecuteReader();
            DataTable dt9 = new DataTable();
            DropDownList9.Items.Clear();
            ListItem newItem9 = new ListItem();
            newItem9.Text = "<เลือกจังหวัด>";
            newItem9.Value = "0";
            DropDownList9.Items.Add(newItem9);

            while (reader9.Read())
            {
                newItem9 = new ListItem();
                newItem9.Text = reader9["PROVINCE_NAME"].ToString();
                newItem9.Value = reader9["PROVINCE_ID"].ToString();
                DropDownList9.Items.Add(newItem9);
            }
        }

        protected void DropDownList9_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            SqlCommand CmdSql9 = new SqlCommand("SELECT AMPHUR_ID,AMPHUR_NAME FROM [amphur] WHERE PROVINCE_ID=@PROVINCE_ID", conn);

            CmdSql9.Parameters.AddWithValue("@PROVINCE_ID", DropDownList9.SelectedValue);
            conn.Open();
            SqlDataReader reader9 = CmdSql9.ExecuteReader();
            DataTable dt10 = new DataTable();
            DropDownList10.Items.Clear();
            ListItem newItem10 = new ListItem();
            newItem10.Text = "<เลือกอำเภอ>";
            newItem10.Value = "0";
            DropDownList10.Items.Add(newItem10);

            while (reader9.Read())
            {
                newItem10 = new ListItem();
                newItem10.Text = reader9["AMPHUR_NAME"].ToString();
                newItem10.Value = reader9["AMPHUR_ID"].ToString();
                DropDownList10.Items.Add(newItem10);
            }
        }

        protected void DropDownList10_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            SqlCommand CmdSql11 = new SqlCommand("SELECT DISTRICT_CODE,DISTRICT_NAME FROM [district] WHERE AMPHUR_ID=@AMPHUR_ID", conn);

            CmdSql11.Parameters.AddWithValue("@AMPHUR_ID", DropDownList10.SelectedValue);
            conn.Open();
            SqlDataReader reader11 = CmdSql11.ExecuteReader();
            DataTable dt11 = new DataTable();
            DropDownList11.Items.Clear();
            ListItem newItem11 = new ListItem();
            newItem11.Text = "<เลือกอำตำบล>";
            newItem11.Value = "0";
            DropDownList11.Items.Add(newItem11);

            while (reader11.Read())
            {
                newItem11 = new ListItem();
                newItem11.Text = reader11["DISTRICT_NAME"].ToString();
                newItem11.Value = reader11["DISTRICT_CODE"].ToString();
                DropDownList11.Items.Add(newItem11);
            }
        }

        protected void DropDownList11_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click1(object sender, EventArgs e)
        {

            //string date2 DateTime.Now();
            string date2 = TextBox4.Text;
            DateTime convertdatedob = DateTime.Parse(date2);


            SqlCommand CmdSql = new SqlCommand("INSERT INTO [Person] (CID,Title,FirstName,LastName,DOB,Nation,Race,Religion,Foreigner,LiveHomeStatus,EducationStatus,LiveStreetAddress,LiveStreetName,Livetumbon,LiveCity,LiveProvince,LivePostcode,CensusStreetAddress,CensusStreetName,Censustumbon,CensusCity,CensusProvince,CensusPostcode,HomePhone,Mobile,RecordedStaffID,DateTimeUpdate) VALUES (@CID,@Title,@FirstName,@LastName,@DOB,@Nation,@Race,@Religion,@Foreigner,@LiveHomeStatus,@EducationStatus,@LiveStreetAddress,@LiveStreetName,@Livetumbon,@LiveCity,@LiveProvince,@LivePostcode,@CensusStreetAddress,@CensusStreetName,@Censustumbon,@CensusCity,@CensusProvince,@CensusPostcode,@HomePhone,@Mobile,@RecordedStaffID,@DateTimeUpdate)", conn2);
            conn2.Open();
                CmdSql.Parameters.AddWithValue("@CID", TextBox3.Text);
                CmdSql.Parameters.AddWithValue("@DateTimeUpdate", DateTime.Now);

                CmdSql.Parameters.AddWithValue("@Title", DropDownList1.Text);
                CmdSql.Parameters.AddWithValue("@FirstName", TextBox1.Text);
                CmdSql.Parameters.AddWithValue("@LastName", TextBox2.Text);
                CmdSql.Parameters.AddWithValue("@DOB", convertdatedob);
                CmdSql.Parameters.AddWithValue("@Nation", TextBox5.Text);
                CmdSql.Parameters.AddWithValue("@Race", TextBox6.Text);
                CmdSql.Parameters.AddWithValue("@Religion", TextBox8.Text);
                CmdSql.Parameters.AddWithValue("@Foreigner", TextBox19.Text);
                CmdSql.Parameters.AddWithValue("@LiveHomeStatus", DropDownList7.Text);
                CmdSql.Parameters.AddWithValue("@EducationStatus", DropDownList18.Text);
                CmdSql.Parameters.AddWithValue("@LiveStreetAddress", TextBox32.Text);
                CmdSql.Parameters.AddWithValue("@LiveStreetName", TextBox33.Text);
                CmdSql.Parameters.AddWithValue("@Livetumbon", DropDownList6.Text);
                CmdSql.Parameters.AddWithValue("@LiveCity", DropDownList5.Text);
                CmdSql.Parameters.AddWithValue("@LiveProvince", DropDownList4.Text);
                CmdSql.Parameters.AddWithValue("@LivePostcode", TextBox7.Text);
                CmdSql.Parameters.AddWithValue("@CensusStreetAddress", TextBox34.Text);
                CmdSql.Parameters.AddWithValue("@CensusStreetName", TextBox35.Text);
                CmdSql.Parameters.AddWithValue("@Censustumbon", DropDownList11.Text); ;
                CmdSql.Parameters.AddWithValue("@CensusCity", DropDownList10.Text);
                CmdSql.Parameters.AddWithValue("@CensusProvince", DropDownList9.Text);
                CmdSql.Parameters.AddWithValue("@CensusPostcode", TextBox9.Text);
                CmdSql.Parameters.AddWithValue("@HomePhone", TextBox10.Text);
                CmdSql.Parameters.AddWithValue("@Mobile", TextBox11.Text);
                CmdSql.Parameters.AddWithValue("@RecordedStaffID", Label1.Text);
               
            CmdSql.ExecuteNonQuery();
            conn2.Close();


      



            MessageBox("ลงทะเบียนเรียบร้อยครับ");

            Session["CID"] = TextBox3.Text;
            Response.Redirect("~/regfamily.aspx");

        






        }

        protected void Button2_Click(object sender, EventArgs e)
        {



            Session["LastName"] = TextBox2.Text;
            Session["FirstName"] = TextBox1.Text;
            Session["CID"] = TextBox3.Text;
            Response.Redirect("~/regfamily.aspx");



        }
    }
}